/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[InvokeMethods.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package reflection;

import java.lang.reflect.Method;

/**
 * 反射 - 调用方法<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class InvokeMethods {

	public int add(int param1, int param2) {
		return param1 + param2;
	}

	public static String echo(String msg) {
		return "echo: " + msg;
	}

	public static void main(String[] args) throws Exception {
		Class<?> classType = InvokeMethods.class;

		// 构造调用对象
		Object invoker = classType.newInstance();
		// 获得调用的方法
		Method addMethod = classType.getMethod("add", int.class, int.class);
		// 获得调用结果
		Object result = addMethod.invoke(invoker, 33, 55);
		System.out.println(result);

		Method echoMethod = classType.getMethod("echo", new Class[] { String.class });
		// 静态方法的调用者为null
		result = echoMethod.invoke(null, new Object[] { "Hello world" });
		System.out.println((String) result);

		// 反射main方法
		Class<?> anotherClassType = GetSuperClassType.class;
		Method mainMethod = anotherClassType.getMethod("main", String[].class);
		result = mainMethod.invoke(null, new Object[] { new String[] {} });
	}
}
